﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LCWMS
{
    public partial class w_so014_loc : Form
    {
       
        public w_so014_loc()
        {
            InitializeComponent();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dg_location_DoubleClick(object sender, EventArgs e)
        {
            int i_temp;
            if (dg_location.CurrentRow != null)  //说明有选中的行有效
            {
                i_temp = dg_location.CurrentRow.Index;
                //相应的值
                w_so014.dt_save.Rows[0]["so_no"] = (string)dg_location.Rows[i_temp].Cells["batchno"].EditedFormattedValue.ToString().Trim();
                w_so014.dt_save.Rows[0]["so_line_no"] = (string)dg_location.Rows[i_temp].Cells["pallet_no"].EditedFormattedValue.ToString().Trim();
                w_so014.dt_save.Rows[0]["n_ea_cubage"] = (string)dg_location.Rows[i_temp].Cells["n_ea_cubage"].EditedFormattedValue.ToString().Trim();
                w_so014.dt_save.Rows[0]["n_ea_gross"] = (string)dg_location.Rows[i_temp].Cells["n_ea_gross"].EditedFormattedValue.ToString().Trim();
                w_so014.dt_save.Rows[0]["n_ea_net"] = (string)dg_location.Rows[i_temp].Cells["n_ea_net"].EditedFormattedValue.ToString().Trim();
                if (System.Convert.ToDecimal(dg_location.Rows[i_temp].Cells["qty"].EditedFormattedValue.ToString().Trim()) > System.Convert.ToDecimal(w_so014.dt_save.Rows[0]["old_qty"]))
                {
                    w_so014.dt_save.Rows[0]["qty"] = w_so014.dt_save.Rows[0]["old_qty"];
                }
                else  //库存数量小于等于缺货数量
                {
                    w_so014.dt_save.Rows[0]["qty"] = dg_location.Rows[i_temp].Cells["qty"].EditedFormattedValue.ToString().Trim();
                    w_so014.dt_save.Rows[0]["old_qty"] = System.Convert.ToString(dg_location.Rows[i_temp].Cells["qty"].EditedFormattedValue.ToString().Trim());
                }
                //关闭窗体
                this.Close();
            }
        }

        private void w_so014_loc_Load(object sender, EventArgs e)
        {
            //根据库位得到可用批次和托盘的数量
            string str_sql;
            DataSet ds_avail;
            //得到可用库存

            if (main.dbtype == "SQL")
            {

                str_sql = "select a.consignor,a.sku,a.location_code,a.batchno,a.pallet_no,(isnull(a.stock_qty_m,0) - isnull(a.freeze_qty_m,0)) as qty," +
                          "round(isnull(a.cubage,0) / isnull(a.stock_qty_m,1),8) n_ea_cubage,round(isnull(a.gross_weight,0) / isnull(a.stock_qty_m,1),8) n_ea_gross,round(isnull(a.net_weight,0) / isnull(a.stock_qty_m,1),8) n_ea_net,b.lot_attribute2 " +
                          "from " + main.g_space + ".inv_sku_pallet a," + main.g_space + ".inv_sku_batchno b where a.inv_org = b.inv_org and a.batchno = b.batchno and a.location_code = '" + w_so014.dt_save.Rows[0]["location_code"].ToString() + "' and a.consignor='" +
                         w_so014.dt_save.Rows[0]["consignor"].ToString() + "' and a.inv_org = '"+main.g_org+"' and a.sku='" + w_so014.dt_save.Rows[0]["sku"].ToString() + "' and (isnull(a.stock_qty_m,0) - isnull(a.freeze_qty_m,0)) > 0 order by qty,b.lot_attribute2";
                ds_avail = main.dbservice.Sql_exec_selectsql(str_sql);
            }
            else
            {
                str_sql = "select a.consignor,a.sku,a.location_code,a.batchno,a.pallet_no,(nvl(a.stock_qty_m,0) - nvl(a.freeze_qty_m,0)) as qty," +
                          "round(nvl(a.cubage,0) / nvl(a.stock_qty_m,1),8) n_ea_cubage,round(nvl(a.gross_weight,0) / nvl(a.stock_qty_m,1),8) n_ea_gross,round(nvl(a.net_weight,0) / nvl(a.stock_qty_m,1),8) n_ea_net,b.lot_attribute2 " +
                          "from " + main.g_space + ".inv_sku_pallet a," + main.g_space + ".inv_sku_batchno b where a.inv_org = b.inv_org and a.batchno = b.batchno and a.location_code = '" + w_so014.dt_save.Rows[0]["location_code"].ToString() + "' and a.consignor='" +
                            w_so014.dt_save.Rows[0]["consignor"].ToString() + "' and a.inv_org = '" + main.g_org + "' and a.sku='" + w_so014.dt_save.Rows[0]["sku"].ToString() + "' and (nvl(a.stock_qty_m,0) - nvl(a.freeze_qty_m,0)) > 0 order by qty,b.lot_attribute2";
                ds_avail = main.dbservice.oracle_exec_selectsql(str_sql);
            }
            dg_location.DataSource = ds_avail.Tables[0];
        }
    }
}